#include "impl.h"

#include "../format/txt.h"

#include <QFileInfo>
#include <QHash>
#include <cassert>

namespace Book
{
namespace Loader
{

Impl::Impl ()
{
    RegisterBookFormat<Format::TXT>();
}

template <typename _BookFormat>
void Impl::RegisterBookFormat ()
{
    QSharedPointer<_BookFormat> format (new _BookFormat());

    foreach (QString ext, format->GetFileExtensionsList())
        m_FormatReaders[ext] = format;
}

Format::I& Impl::GetFormatReader (const QString& _fileName) const
{
    QString ext = QFileInfo(_fileName).suffix().toUpper();
    if (m_FormatReaders.contains(ext))
        return *m_FormatReaders[ext].data();
    else
    {
        assert(m_FormatReaders.contains("TXT"));
        return *m_FormatReaders["TXT"].data();
    }
}

DataPtr Impl::OpenBook(const Book::FileInfo& _file) const
{
    Format::I& reader = GetFormatReader(_file.GetFileName());
    return reader.ReadBook(_file);
}


}
}

